#!/usr/bin/Rscript
os <<- Sys.info()['sysname']

if(os == "Windows"){
  setwd("F:/cuffdiff/Reactome")
}else if(os == Linux){
  setwd("/home/zhang/BMK/test/cuffdiff/Reactome")
}


read.kegg <- function(input, num=1){
  if(!require(openxlsx)){
    install.aocakges("openxlsx")
    if(!require(openxlsx)){
      stop("openxlsx install failed")
    }
  }
  input <- read.xlsx(as.character(input), sheet = num)
  
  # 用来计算RichFactor，用的是其中的BgRatio
  caculate.richfactor <- function(input){
    value <- unlist(strsplit(input[4], "/"))
    return(as.numeric(as.character(value[1]))/as.numeric(as.character(value[2])))
  }
  
  input$RichFactor <- apply(input, 1, caculate.richfactor)
  return(input)
}

# 根据clusterProfile包的结果，画kegg的图
draw.clusterprofile <- function(input)
{
  if(!require(ggplot2)){
    install.packages("ggplot2")
    if(!require(ggplot2)){
      stop("ggplot2 install failed")
    }
  }
  
  # window specific
  # 改变字体为Times New Roman
  windowsFonts(NRM = windowsFont("Times New Roman"))
  old_theme <- theme_update(axis.text = element_text(family = "NRM", size = 18, color = "black"),
                            legend.key.size = unit(1.2, "cm"),
                            axis.title = element_text(family = "NRM", size = 18),
                            legend.text = element_text(family = "NRM", size = 18))
  
  p <- ggplot(input,
              aes(x = RichFactor,
                  y = reorder(Description, qvalue, sort),
                  colour = qvalue,
                  size = Count)
  ) + geom_point() + scale_colour_gradientn(colours=rainbow(4)) + ylab("Description")
  return(p)
}

tem <- read.kegg("reactomePA.xlsx", 1)
name <- c("all.tiff")
tiff(name, units = "in", width = 20, height = 15, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()

tem <- read.kegg("reactomePA.xlsx", 2)
name <- c("cerna_mrna.tiff")
tiff(name, units = "in", width = 15, height = 15, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()


tem <- read.kegg("superdiffreactome.xlsx", 1)
name <- c("hsa-miR-3620-5p.tiff")
tiff(name, units = "in", width = 15, height = 10, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()


tem <- read.kegg("superdiffreactome.xlsx", 2)
name <- c("hsa-miR-3916.tiff")
tiff(name, units = "in", width = 8, height = 8, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()


tem <- read.kegg("superdiffreactome.xlsx", 3)
name <- c("hsa-miR-4763-3p.tiff")
tiff(name, units = "in", width = 12, height = 8, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()


tem <- read.kegg("superdiffreactome.xlsx", 5)
name <- c("hsa-miR-6825-5p.tiff")
tiff(name, units = "in", width = 12, height = 8, compression = "lzw", res = 600)
draw.clusterprofile(tem) + theme(text = element_text(size=20))
dev.off()